<template>
  <a-modal
    width="600px"
    v-model="modalVisible"
    :destroyOnClose="true"
    :maskClosable="false"
    :title="title"
    @cancel="cancel"
  >
    <template slot="footer">
      <a-button key="back" @click="cancel" :disabled="loading"> 取消 </a-button>
      <a-button key="submit" @click="save" type="primary" :loading="loading">
        确定
      </a-button>
    </template>
    <a-spin :spinning="spinning">
      <a-form-model
        ref="form"
        :model="form"
        :rules="rules"
        :label-col="config.labelCol"
        :wrapper-col="config.wrapperCol"
      >
        <a-row>
          <a-col>
            <a-form-model-item ref="parentName" label="上级" prop="parentName">
              <a-tree-select
                placeholder="请选择上级"
                allow-clear
                v-model="form.parentName"
                show-search
                style="width: 100%"
                treeNodeFilterProp="title"
                :tree-data="data"
                :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
                @select="treeSelect"
              >
              </a-tree-select>
            </a-form-model-item>
            <a-form-model-item ref="name" label="名称" prop="name">
              <a-input v-model="form.name" placeholder="请输入名称" />
            </a-form-model-item>
            <a-form-model-item ref="value" label="值" prop="value">
              <a-input v-model="form.value" placeholder="请输入值" />
            </a-form-model-item>

            <a-form-model-item ref="orderNo" label="排序号" prop="orderNo">
              <a-input-number
                id="inputNumber"
                style="width: 100%"
                placeholder="请输入排序号"
                v-model="form.orderNo"
                :min="0"
                :max="999"
              />
            </a-form-model-item>
            <a-form-model-item label="禁用" prop="isFreeze">
              <a-switch v-model="form.isFreeze" />
            </a-form-model-item>
            <a-form-model-item label="备注" prop="remark">
              <a-input
                v-model="form.remark"
                type="textarea"
                placeholder="请输入备注"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import { save, findById } from "@/services/system/dictionary/edit";
import { emptyGuid } from "@/utils/util";
export default {
  name: "edit",
  data() {
    return {
      config: {
        labelCol: { span: 4 },
        wrapperCol: { span: 18 },
      },
      form: {
        dictionaryId: emptyGuid(),
        parentId: emptyGuid(),
        parentName: null,
        name: "",
        value: null,
        orderNo: 0,
        isFreeze: false,
        remark: "",
      },

      rules: {
        name: [
          {
            required: true,
            message: "请输入名称",
            trigger: "blur",
          },
        ],
      },

      modalVisible: this.visible,
      loading: false,
      spinning: false,
    };
  },

  watch: {
    visible(val) {
      this.modalVisible = val;
    },
  },

  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    data: {
      type: Array,
    },
    title: {
      type: String
    },
  },

  methods: {
    /**
     * 树选择
     */
    treeSelect(electedKeys, e) {
      this.form.parentName = e.title;
      this.form.parentId = e.eventKey;
    },

    /**
     * 取消
     */
    cancel() {
      this.reset();
      this.$emit("close");
    },

    /**
     * 保存
     */
    save() {
      let _this = this;
      this.$refs.form.validate((valid) => {
        if (valid) {
          _this.loading = true;
          _this.spinning = true;
          save(_this.form).then(function(result) {
            if (result.code === _this.$resultCode.success) {
              _this.$message.success(result.msg);
              _this.reset();
              _this.$emit("save", result);
            } else {
              _this.$message.error(result.msg);
            }
            _this.loading = false;
            _this.spinning = false;
          });
        } else {
          return false;
        }
      });
    },

    /**
     * 重置
     */
    reset() {
      this.loading = false;
      this.$refs.form.resetFields();
      this.form.dictionaryId = emptyGuid();
    },

    /**
     * 赋予父级
     */
    setParent(parentId, parentName) {
      this.form.parentId = parentId;
      this.form.parentName = parentName;
    },

    /**
     * 根据Id查找
     */
    find(dictionaryId) {
      let _this = this;
      _this.spinning = true;
      findById(dictionaryId).then(function(result) {
        let form = result.data;
        _this.form.dictionaryId = form.dictionaryId;
        _this.form.parentName = form.parentName;
        _this.form.parentId =
          _this.form.parentName == "" ? emptyGuid() : form.parentId;
        _this.form.name = form.name;
        _this.form.value = form.value;
        _this.form.orderNo = form.orderNo;
        _this.form.isFreeze = form.isFreeze;
        _this.form.remark = form.remark;
        _this.spinning = false;
      });
    },
  },
};
</script>

<style lang="less" scoped></style>
